<?php

	//
	// Users
	//

	$qr_addUser = "INSERT INTO WBS_USER (U_ID, C_ID, U_PASSWORD, U_STATUS, U_ACCESSTYPE) values
					('!U_ID!', '!C_ID!', '!U_PASSWORD!', '!U_STATUS!', '!U_ACCESSTYPE!')";

	$qr_selectUser = "SELECT U.*, C.* FROM WBS_USER U, CONTACT C WHERE U.U_ID='!U_ID!' AND C.C_ID=U.C_ID";

	$qr_selectSpecificUsers = "SELECT U.*, C.* FROM WBS_USER U, CONTACT C WHERE U.U_ID in (%s) AND C.C_ID=U.C_ID ORDER BY %s";

	$qr_modifyUserStatus = "UPDATE WBS_USER SET U_STATUS='!U_STATUS!', U_ACCESSTYPE='!U_ACCESSTYPE!' WHERE U_ID='!U_ID!'";

	$qr_modifyUserStatusOnly = "UPDATE WBS_USER SET U_STATUS='!U_STATUS!' WHERE U_ID='!U_ID!'";

	$qr_setUserAccessType = "UPDATE WBS_USER SET U_ACCESSTYPE='!U_ACCESSTYPE!' WHERE U_ID='!U_ID!'";

	$qr_modifyUser_pwd = "UPDATE WBS_USER SET U_PASSWORD='!U_PASSWORD!', U_STATUS='!U_STATUS!', U_ACCESSTYPE='!U_ACCESSTYPE!' WHERE U_ID='!U_ID!'";

	$qr_modifyPassword = "UPDATE WBS_USER SET U_PASSWORD='!U_PASSWORD!' WHERE U_ID='!U_ID!'";

	$qr_selectUserLoginInfo = "SELECT U_ID, U_PASSWORD, U_STATUS FROM WBS_USER WHERE U_ID='!U_ID!'";
	
	$qr_selectUserEmailInfo = "SELECT WBS_USER.U_ID, CONTACT.C_EMAILADDRESS, CONTACT.C_FIRSTNAME, CONTACT.C_LASTNAME, WBS_USER.U_STATUS FROM WBS_USER INNER JOIN CONTACT ON (WBS_USER.C_ID=CONTACT.C_ID) WHERE WBS_USER.U_ID='!U_ID!'";

	$qr_selectUsers_2Status = "SELECT U.*, CA.* FROM WBS_USER U, CONTACT CA WHERE CA.C_ID=U.C_ID AND (U.U_STATUS=%s OR U.U_STATUS=%s) ORDER BY %s";

	$qr_selectUsers_1Status = "SELECT U.*, CA.* FROM WBS_USER U, CONTACT CA WHERE CA.C_ID=U.C_ID AND U_STATUS=%s ORDER BY %s";
	
	$qr_selectUsers_Support = "SELECT U.*, CA.* FROM UGROUP_USER GR, WBS_USER U, CONTACT CA WHERE GR.UG_ID in (%s) AND GR.U_ID = U.U_ID AND CA.C_ID=U.C_ID AND U.U_STATUS=%s GROUP BY GR.U_ID ORDER BY %s  ";

	$qr_selectAllUsers = "SELECT * FROM WBS_USER ORDER BY U_ID";

	$qr_selectUsersCount = "SELECT COUNT(*) AS CNT FROM WBS_USER WHERE U_ID='!U_ID!'";

	$qr_selectUserIDs = "SELECT U_ID FROM WBS_USER ORDER BY U_ID";

	$qr_selectUsersSettings = "SELECT U_ID, U_SETTINGS FROM WBS_USER WHERE U_STATUS=%s ORDER BY U_ID";

	$qr_deleteUser = "DELETE FROM WBS_USER WHERE U_ID='!U_ID!'";

	$qr_saveUserStatus = "UPDATE WBS_USER SET U_STATUS='!U_STATUS!' WHERE U_ID='!U_ID!'";

	$qr_selectGlobalUserCount = "SELECT COUNT(*) FROM WBS_USER WHERE U_STATUS <> '!U_STATUS!'";

	$qr_selectGlobalUserCountExcl = "SELECT COUNT(*) FROM WBS_USER WHERE U_STATUS <> '!U_STATUS!' AND U_ID <> '!U_ID!'";

	$qr_selectuserstatuscount = "SELECT COUNT(*) FROM WBS_USER WHERE U_STATUS='!U_STATUS!'";

	$qr_selectglobalusercount = "SELECT COUNT(*) FROM WBS_USER";

	$qr_selectUserContact = "SELECT C_ID FROM WBS_USER WHERE U_ID='!U_ID!'";

	$qr_selectUnsortedFolder = "SELECT CF_ID FROM CFOLDER WHERE CF_NAME=\"_Unsorted\" AND CF_ID_PARENT=\"ROOT\"";

	//
	// User groups
	//

	$qr_select_ugroups = "SELECT * FROM UGROUP ORDER BY UG_NAME";

	$qr_selectugcontent = "SELECT U.*, C.* FROM WBS_USER U, CONTACT C WHERE U.U_STATUS='!U_STATUS!' AND C.C_ID=U.C_ID ORDER BY %s";

	$qr_selectugenumcontent = "SELECT U.*, C.* FROM WBS_USER U, CONTACT C, UGROUP_USER UGU WHERE U.U_ID=UGU.U_ID AND UGU.UG_ID = '!UG_ID!' AND C.C_ID=U.C_ID AND U.U_STATUS <> '!U_STATUS!' ORDER BY %s";

	$qr_selectusers = "SELECT U.*, C.* FROM WBS_USER U, CONTACT C WHERE U.U_STATUS in (%s) AND C.C_ID=U.C_ID ORDER BY %s";

	$qr_selectMaxUGID = "SELECT MAX(UG_ID) FROM UGROUP";

	$qr_insertusergroup = "INSERT INTO UGROUP(UG_ID, UG_NAME, UG_SETTINGS) VALUES('!UG_ID!', '!UG_NAME!', '!UG_SETTINGS!')";

	$qr_selectugroup = "SELECT * FROM UGROUP WHERE UG_ID='!UG_ID!'";

	$qr_selectugroupsettings = "SELECT UG_SETTINGS FROM UGROUP WHERE UG_ID='!UG_ID!'";

	$qr_updateugroup = "UPDATE UGROUP SET UG_NAME='!UG_NAME!' WHERE UG_ID='!UG_ID!'";

	$qr_deleteugroup = "DELETE FROM UGROUP WHERE UG_ID='!UG_ID!'";

	$qr_deletegroupusers = "DELETE FROM UGROUP_USER WHERE UG_ID='!UG_ID!'";

	$qr_insertgroupusers = "INSERT INTO UGROUP_USER(UG_ID, U_ID) VALUES ('!UG_ID!', '!U_ID!')";

	$qr_selectusergroups = "SELECT UGU.* FROM UGROUP_USER UGU, WBS_USER U WHERE UGU.U_ID='!U_ID!' AND U.U_ID=UGU.U_ID AND U.U_STATUS <> '!U_STATUS!'";

	$qr_selectugcount = "SELECT COUNT(*) FROM UGROUP_USER UG, WBS_USER U WHERE UG.UG_ID='!UG_ID!' AND U.U_ID=UG.U_ID AND U.U_STATUS <> '!U_STATUS!'";

	$qr_updateGroupSettings = "UPDATE UGROUP SET UG_SETTINGS='!UG_SETTINGS!' WHERE UG_ID='!UG_ID!'";

	$qr_selectUserGroupCount = "SELECT COUNT(*) FROM UGROUP WHERE UG_ID='!UG_ID!'";

	$qr_deleteGroupScreenAccess = "DELETE FROM UG_ACCESSRIGHTS WHERE AR_ID='!UG_ID!'";

	$qr_deleteGroupFolderAccess = "DELETE FROM %s WHERE %s = '!UG_ID!'";

	$qr_selectGroupName = "SELECT UG_NAME FROM UGROUP WHERE UG_ID='!UG_ID!'";

	//
	//	User screen access
	//

	$qr_deleteUserScreenAccess = "DELETE FROM U_ACCESSRIGHTS WHERE AR_ID='!U_ID!'";

	//
	//	Company information
	//

	$qr_selectCompanyInfo = "SELECT * FROM COMPANY";

	$qr_selectCompanyName = "SELECT CON_NAME FROM COMPANY";

	$qr_insertCompanyInfo = "INSERT INTO COMPANY(COM_NAME,COM_ADDRESSSTREET,COM_ADDRESSCITY,COM_ADDRESSSTATE,COM_ADDRESSZIP,COM_ADDRESSCOUNTRY,COM_CONTACTPERSON,COM_EMAIL,COM_PHONE,COM_FAX) values ('!COM_NAME!','!COM_ADDRESSSTREET!','!COM_ADDRESSCITY!','!COM_ADDRESSSTATE!','!COM_ADDRESSZIP!','!COM_ADDRESSCOUNTRY!','!COM_CONTACTPERSON!','!COM_EMAIL!','!COM_PHONE!','!COM_FAX!')";

	$qr_deleteCompanyInfo = "DELETE FROM COMPANY";

	//
	//	User settings
	//

	$qr_selectUserSettings = "SELECT U_SETTINGS FROM WBS_USER WHERE U_ID='!U_ID!'";

	$qr_updateUserSettings = "UPDATE WBS_USER SET U_SETTINGS='!U_SETTINGS!' WHERE U_ID='!U_ID!'";

	//
	// Currency List
	//

	$qr_select_currency_list = "SELECT * FROM CURRENCY ORDER BY CUR_ID";

	$qr_insert_currency = "INSERT INTO CURRENCY(CUR_ID, CUR_NAME) VALUES ('!CUR_ID!', '!CUR_NAME!')";

	$qr_select_currency = "SELECT * FROM CURRENCY WHERE CUR_ID='!CUR_ID!'";

	$qr_update_currency = "UPDATE CURRENCY SET CUR_NAME='!CUR_NAME!' WHERE CUR_ID='!CUR_ID!'";

	$qr_delete_currency = "DELETE FROM CURRENCY WHERE CUR_ID='!CUR_ID!'";

	//
	// Host queries
	//

	$qr_host_addUser = "INSERT INTO WBS_USER (U_ID, C_ID, U_PASSWORD, U_STATUS, U_SETTINGS, U_ACCESSTYPE) values ('!U_ID!', '!C_ID!', '!U_PASSWORD!', '!U_STATUS!', '!U_SETTINGS!', '!U_ACCESSTYPE!')";

	$qr_host_addCompanyData = "INSERT INTO COMPANY (COM_NAME, COM_CONTACTPERSON, COM_EMAIL) values ('!COM_NAME!', '!COM_CONTACTPERSON!', '!COM_EMAIL!')";

	$qr_host_addUserAccess = "INSERT INTO U_ACCESSRIGHTS(AR_UD, AR_PATH, AR_OBJECT_ID, AR_VALUE) VALUES ('!U_ID!', CONCAT('/ROOT/','!APP_ID!','/SCREENS'), '!SCR_ID!', 1)";

	$qr_host_dropDatabase = "DROP DATABASE %s";

	$qr_host_deleteUser = "DELETE FROM mysql.user WHERE Host='%s' and User='%s'";

	$qr_host_revokePrivileges = "REVOKE ALL PRIVILEGES ON %s.* FROM %s@%s";

	$qr_host_addFolder = "INSERT INTO CFOLDER(CF_ID, CF_ID_PARENT, CF_NAME, CT_ID) VALUES ('1.', 'ROOT', '_Unsorted', 'CON')";

	$qr_host_addContactType = "INSERT INTO CTYPE(CT_ID, CT_NAME, CT_SETTINGS) VALUES ('CON', 'Contact', '!CT_SETTINGS!')";

	$qr_host_addContact = "INSERT INTO CONTACT(C_ID, CT_ID, CF_ID, C_CREATEDATETIME, C_CREATECID, C_FULLNAME, C_FIRSTNAME, C_LASTNAME, C_EMAILADDRESS) VALUES ('!C_ID!', '!CT_ID!', '!CF_ID!', '!C_CREATEDATETIME!', '!C_CREATECID!', '!C_FULLNAME!', '!C_FIRSTNAME!', '!C_LASTNAME!', '!C_EMAILADDRESS!')";
	
	$qr_host_addContactEmail = "INSERT INTO EMAIL_CONTACT SET EC_ID = '!C_ID!', EC_EMAIL = '!C_EMAILADDRESS!'";
	
	$qr_host_deleteDemoUser = "DELETE FROM WBS_USER WHERE U_ID='DEMO@WEBASYST.NET'";
	
	$qr_host_deleteDemoUserContact = "DELETE FROM CONTACT WHERE C_EMAILADDRESS = 'demo@webasyst.net'";
	
	$qr_host_grantAdminAccess = "INSERT INTO U_ACCESSRIGHTS(AR_ID, AR_PATH, AR_OBJECT_ID, AR_VALUE) VALUES ('!U_ID!', '/ROOT', 'ADMINISTRATOR', 2147483647)";

	//
	// Tree support functions
	//

//	$qr_tree_selectUserFolders = "SELECT DF.*, DA.ACCESS_RIGHT_FIELD FROM TREE_FOLDER_TABLE DF LEFT JOIN TREE_ACCESS_TABLE DA ON DF.FOLDER_ID_FIELD=DA.FOLDER_ID_FIELD AND DA.USER_ID_FIELD='!USER_ID_FIELD!' WHERE DF.FOLDER_PARENT_FIELD='!FOLDER_PARENT_FIELD!' AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' ORDER BY DF.FOLDER_NAME_FIELD";

//	$qr_tree_selectGroupFolders = "SELECT DF.*, MAX(DA.GROUP_ACCESS_RIGHT_FIELD) AS ACCESS_RIGHT_FIELD FROM TREE_FOLDER_TABLE DF LEFT JOIN UGROUP_USER UGU ON UGU.U_ID='!USER_ID_FIELD!' LEFT JOIN TREE_GROUP_ACCESS_TABLE DA ON DA.FOLDER_ID_FIELD=DF.FOLDER_ID_FIELD AND DA.GROUP_ID_FIELD=UGU.GROUP_ID_FIELD WHERE DF.FOLDER_PARENT_FIELD='!FOLDER_PARENT_FIELD!' AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' GROUP BY DF.FOLDER_ID_FIELD ORDER BY DF.FOLDER_NAME_FIELD";

//	$qr_tree_selectUserSummaryFolders = "SELECT DF.*, MAX(DA.GROUP_ACCESS_RIGHT_FIELD) AS GROUP_RIGHTS, DAC.ACCESS_RIGHT_FIELD AS USER_RIGHTS FROM TREE_FOLDER_TABLE DF LEFT JOIN UGROUP_USER UGU ON UGU.U_ID='!USER_ID_FIELD!' LEFT JOIN TREE_GROUP_ACCESS_TABLE DA ON DA.FOLDER_ID_FIELD=DF.FOLDER_ID_FIELD AND DA.GROUP_ID_FIELD=UGU.GROUP_ID_FIELD LEFT JOIN TREE_ACCESS_TABLE DAC ON DAC.FOLDER_ID_FIELD=DF.FOLDER_ID_FIELD AND DAC.USER_ID_FIELD='!USER_ID_FIELD!' AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' WHERE DF.FOLDER_PARENT_FIELD='!FOLDER_PARENT_FIELD!' GROUP BY DF.FOLDER_ID_FIELD ORDER BY DF.FOLDER_NAME_FIELD";

	$qr_tree_selectParentFolders = "SELECT DF.* FROM TREE_FOLDER_TABLE DF WHERE FOLDER_PARENT_FIELD='!FOLDER_PARENT_FIELD!' AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' ORDER BY ORDER_BY_STR";

	$qr_tree_selectParentFoldersAnyStatus = "SELECT DF.* FROM TREE_FOLDER_TABLE DF WHERE FOLDER_PARENT_FIELD='!FOLDER_PARENT_FIELD!' ORDER BY ORDER_BY_STR";

	$qr_tree_selectAllUserFolders = "SELECT DF.*, DA.ACCESS_RIGHT_FIELD FROM TREE_FOLDER_TABLE DF LEFT JOIN TREE_ACCESS_TABLE DA ON DF.FOLDER_ID_FIELD=DA.FOLDER_ID_FIELD AND DA.USER_ID_FIELD='!USER_ID_FIELD!' WHERE DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' ORDER BY DF.FOLDER_NAME_FIELD";

	$qr_tree_selectAllGroupFolders = "SELECT DF.*, MAX(DA.GROUP_ACCESS_RIGHT_FIELD) AS ACCESS_RIGHT_FIELD FROM TREE_FOLDER_TABLE DF LEFT JOIN UGROUP_USER UGU ON UGU.U_ID='!USER_ID_FIELD!' LEFT JOIN TREE_GROUP_ACCESS_TABLE DA ON DA.FOLDER_ID_FIELD=DF.FOLDER_ID_FIELD AND DA.GROUP_ID_FIELD=UGU.GROUP_ID_FIELD WHERE DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' GROUP BY DF.FOLDER_ID_FIELD ORDER BY DF.FOLDER_NAME_FIELD";

	$qr_tree_selectAllUserSummaryFolders = "SELECT DF.* , ( BIT_OR( DA.AR_VALUE ) | IF (DAC.AR_VALUE IS NULL , 0, DAC.AR_VALUE) ) AS USER_RIGHTS FROM TREE_FOLDER_TABLE DF LEFT JOIN UGROUP_USER UGU ON UGU.U_ID = '!USER_ID_FIELD!' LEFT JOIN UG_ACCESSRIGHTS DA ON DA.AR_OBJECT_ID = DF.FOLDER_ID_FIELD AND DA.AR_ID = UGU.UG_ID AND DA.AR_PATH = 'RIGHTS_PATH' LEFT JOIN U_ACCESSRIGHTS DAC ON DAC.AR_OBJECT_ID = DF.FOLDER_ID_FIELD AND DAC.AR_ID = '!USER_ID_FIELD!' AND DAC.AR_PATH = 'RIGHTS_PATH' WHERE DF.FOLDER_STATUS_FIELD = '!FOLDER_STATUS_FIELD!' GROUP BY DF.FOLDER_ID_FIELD ORDER BY ORDER_BY_STR";
	
	$qr_tree_selectAllUserSummaryFoldersWithLinked = "
		SELECT 
			DF.* , 
			IF (NOT(AL.LINK_AR_PATH IS NULL),
				BIT_OR( DA2.AR_VALUE ) | IF (DAC2.AR_VALUE IS NULL , 0, DAC2.AR_VALUE),
				BIT_OR( DA.AR_VALUE ) | IF (DAC.AR_VALUE IS NULL , 0, DAC.AR_VALUE)) 
			AS USER_RIGHTS
		FROM TREE_FOLDER_TABLE DF 
			LEFT JOIN UGROUP_USER UGU ON UGU.U_ID = '!USER_ID_FIELD!' 
			LEFT JOIN UG_ACCESSRIGHTS DA ON DA.AR_OBJECT_ID = DF.FOLDER_ID_FIELD AND DA.AR_ID = UGU.UG_ID AND DA.AR_PATH = 'RIGHTS_PATH' 
			LEFT JOIN U_ACCESSRIGHTS DAC ON DAC.AR_OBJECT_ID = DF.FOLDER_ID_FIELD AND DAC.AR_ID = '!USER_ID_FIELD!' AND DAC.AR_PATH = 'RIGHTS_PATH' 
			LEFT JOIN ACCESSRIGHTS_LINK AL ON AL.AR_PATH='RIGHTS_PATH' AND (AL.AR_OBJECT_ID=DF.FOLDER_ID_FIELD)
			LEFT JOIN U_ACCESSRIGHTS DAC2 ON (DAC2.AR_OBJECT_ID = AL.LINK_AR_OBJECT_ID AND DAC2.AR_PATH = AL.LINK_AR_PATH AND DAC2.AR_ID = '!USER_ID_FIELD!' )
			LEFT JOIN UG_ACCESSRIGHTS DA2 ON (DA2.AR_OBJECT_ID = AL.LINK_AR_OBJECT_ID AND DA2.AR_PATH = AL.LINK_AR_PATH AND DA2.AR_ID = UGU.UG_ID )
		WHERE DF.FOLDER_STATUS_FIELD = '!FOLDER_STATUS_FIELD!' 
		GROUP BY DF.FOLDER_ID_FIELD 
		ORDER BY ORDER_BY_STR";
	
	$qr_tree_selectAccessrightsLink = "SELECT * FROM ACCESSRIGHTS_LINK WHERE AR_PATH='!AR_PATH!' AND AR_OBJECT_ID='!AR_OBJECT_ID!'";

//	$qr_tree_selectAllUserSummaryFolders = "SELECT DF.*, MAX(DA.GROUP_ACCESS_RIGHT_FIELD) AS GROUP_RIGHTS, DAC.ACCESS_RIGHT_FIELD AS USER_RIGHTS FROM TREE_FOLDER_TABLE DF LEFT JOIN UGROUP_USER UGU ON UGU.U_ID='!USER_ID_FIELD!' LEFT JOIN TREE_GROUP_ACCESS_TABLE DA ON DA.FOLDER_ID_FIELD=DF.FOLDER_ID_FIELD AND DA.GROUP_ID_FIELD=UGU.GROUP_ID_FIELD LEFT JOIN TREE_ACCESS_TABLE DAC ON DAC.FOLDER_ID_FIELD=DF.FOLDER_ID_FIELD AND DAC.USER_ID_FIELD='!USER_ID_FIELD!' AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' GROUP BY DF.FOLDER_ID_FIELD ORDER BY DF.FOLDER_NAME_FIELD";

	$qr_tree_selectAllParentFolders = "SELECT DF.* FROM TREE_FOLDER_TABLE DF WHERE DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' ORDER BY ORDER_BY_STR";

	$qr_tree_countFoldersDocuments = "SELECT DF.FOLDER_ID_FIELD, COUNT( DL.FOLDER_ID_FIELD ) AS DOCCOUNT FROM TREE_DOCUMENT_TABLE DL, TREE_FOLDER_TABLE DF WHERE DL.FOLDER_ID_FIELD = DF.FOLDER_ID_FIELD GROUP BY FOLDER_ID_FIELD";

	$qr_tree_selectFolderDocCount = "SELECT COUNT(*) FROM TREE_DOCUMENT_TABLE WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!' AND DOCUMENT_STATUS_FIELD='!DOCUMENT_STATUS_FIELD!'";

	$qr_tree_selectRecycledDocCount = "SELECT COUNT(*) FROM TREE_DOCUMENT_TABLE DL WHERE DL.DOCUMENT_STATUS_FIELD='!DOCUMENT_STATUS_FIELD!' AND DL.DOCUMENT_MODIFIED_UID_FIELD='!DOCUMENT_MODIFIED_UID_FIELD!'";

	$qr_tree_selectUsersRights = "SELECT U.U_ID, DA.FOLDER_ID_FIELD, DA.ACCESS_RIGHT_FIELD, C.C_LASTNAME, C.C_FIRSTNAME, C.C_MIDDLENAME FROM WBS_USER U, TREE_ACCESS_TABLE DA, CONTACT C WHERE C.C_ID=U.C_ID AND (U.U_STATUS = 0 OR U.U_STATUS = 2) AND DA.USER_ID_FIELD=U.U_ID ORDER BY U.U_ID";

	$qr_tree_selectGroupsRights = "SELECT U.UG_ID, DA.FOLDER_ID_FIELD, DA.GROUP_ACCESS_RIGHT_FIELD AS ACCESS_RIGHT_FIELD, U.UG_NAME FROM UGROUP U, TREE_GROUP_ACCESS_TABLE DA WHERE DA.GROUP_ID_FIELD=U.UG_ID ORDER BY U.UG_ID";

	$qr_tree_selectUserRights = "SELECT U.U_ID, DA.FOLDER_ID_FIELD, DA.ACCESS_RIGHT_FIELD, C.C_LASTNAME, C.C_FIRSTNAME, C.C_MIDDLENAME FROM (WBS_USER U, CONTACT C) LEFT JOIN TREE_ACCESS_TABLE DA ON DA.USER_ID_FIELD=U.U_ID WHERE C.C_ID=U.C_ID AND (U.U_STATUS=0 OR U.U_STATUS=2) AND DA.USER_ID_FIELD='!USER_ID_FIELD!' ORDER BY U.U_ID";

	$qr_tree_selectUserSummaryRights = "SELECT U.U_ID, DGA.FOLDER_ID_FIELD, MAX(DGA.GROUP_ACCESS_RIGHT_FIELD) AS ACCESS_RIGHT_FIELD, UG.UG_NAME, C.C_LASTNAME, C.C_FIRSTNAME, C.C_MIDDLENAME FROM WBS_USER U, TREE_GROUP_ACCESS_TABLE DGA, UGROUP UG, UGROUP_USER UGU, CONTACT C WHERE C.C_ID=U.C_ID AND UGU.U_ID = U.U_ID AND UG.UG_ID = UGU.UG_ID AND DGA.GROUP_ID_FIELD = UG.UG_ID AND (U.U_STATUS = 0 OR U.U_STATUS = 2) AND U.U_ID='!USER_ID_FIELD!' %s GROUP BY DGA.FOLDER_ID_FIELD ORDER BY U.U_ID";

	$qr_tree_selectGroupsSummaryRights = "SELECT DGA.FOLDER_ID_FIELD, MAX(DGA.GROUP_ACCESS_RIGHT_FIELD) AS ACCESS_RIGHT_FIELD FROM TREE_GROUP_ACCESS_TABLE DGA WHERE DGA.GROUP_ID_FIELD IN (%s) GROUP BY DGA.FOLDER_ID_FIELD";

	$qr_tree_selectUsersSummaryRights = "SELECT U.U_ID, DGA.FOLDER_ID_FIELD, MAX(DGA.GROUP_ACCESS_RIGHT_FIELD) AS ACCESS_RIGHT_FIELD, UG.UG_NAME, C.C_LASTNAME, C.C_FIRSTNAME, C.C_MIDDLENAME FROM WBS_USER U, TREE_GROUP_ACCESS_TABLE DGA, UGROUP UG, UGROUP_USER UGU, CONTACT C WHERE C.C_ID=U.C_ID AND UGU.U_ID = U.U_ID AND UG.UG_ID = UGU.UG_ID AND DGA.GROUP_ID_FIELD = UG.UG_ID AND (U.U_STATUS = 0 OR U.U_STATUS = 2) %s GROUP BY DGA.FOLDER_ID_FIELD ORDER BY U.U_ID";

	$qr_tree_selectGroupRights = "SELECT U.UG_ID, DA.FOLDER_ID_FIELD, DA.GROUP_ACCESS_RIGHT_FIELD AS ACCESS_RIGHT_FIELD, U.UG_NAME FROM UGROUP U LEFT JOIN TREE_GROUP_ACCESS_TABLE DA ON DA.GROUP_ID_FIELD=U.UG_ID WHERE DA.GROUP_ID_FIELD='!GROUP_ID_FIELD!' ORDER BY U.UG_ID";

	$qr_tree_selectFolder = "SELECT * FROM TREE_FOLDER_TABLE WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectFolderCount = "SELECT COUNT(*) FROM TREE_FOLDER_TABLE WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_insertFolder = "INSERT INTO TREE_FOLDER_TABLE(FOLDER_ID_FIELD, FOLDER_PARENT_FIELD, FOLDER_NAME_FIELD, FOLDER_STATUS_FIELD) VALUES ('!FOLDER_ID_FIELD!', '!FOLDER_PARENT_FIELD!', '!FOLDER_NAME_FIELD!', '!FOLDER_STATUS_FIELD!')";

	$qr_tree_updateFolderName = "UPDATE TREE_FOLDER_TABLE SET FOLDER_NAME_FIELD='!FOLDER_NAME_FIELD!' WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectmax_ID = "SELECT FOLDER_ID_FIELD FROM TREE_FOLDER_TABLE WHERE FOLDER_PARENT_FIELD='!FOLDER_PARENT_FIELD!' ORDER BY REPLACE(FOLDER_ID_FIELD,'.','')+0 DESC";

	$qr_tree_deleteUserRights = "DELETE FROM U_ACCESSRIGHTS WHERE AR_ID='!USER_ID_FIELD!' AND AR_PATH='!AR_PATH!'";

//	$qr_tree_deleteUserRights = "DELETE FROM TREE_ACCESS_TABLE WHERE USER_ID_FIELD='!USER_ID_FIELD!'";

	$qr_tree_deleteGroupRights = "DELETE FROM UG_ACCESSRIGHTS WHERE AR_ID='!GROUP_ID_FIELD!' AND AR_PATH='!AR_PATH!'";

//	$qr_tree_deleteGroupRights = "DELETE FROM TREE_GROUP_ACCESS_TABLE WHERE GROUP_ID_FIELD='!GROUP_ID_FIELD!'";

	$qr_tree_deleteAllGroupFolderRights = "DELETE FROM UG_ACCESSRIGHTS WHERE AR_OBJECT_ID='!FOLDER_ID_FIELD!' AND AR_PATH='!AR_PATH!'";

//	$qr_tree_deleteAllGroupFolderRights = "DELETE FROM TREE_GROUP_ACCESS_TABLE WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_insertUserRights = "INSERT INTO U_ACCESSRIGHTS(AR_ID, AR_PATH, AR_OBJECT_ID, AR_VALUE, AR_AUX) VALUES('!USER_ID_FIELD!', '!AR_PATH!', '!FOLDER_ID_FIELD!', '!AR_VALUE!', '')";

//	$qr_tree_insertUserRights = "INSERT INTO TREE_ACCESS_TABLE(USER_ID_FIELD, FOLDER_ID_FIELD, ACCESS_RIGHT_FIELD) VALUES('!USER_ID_FIELD!', '!FOLDER_ID_FIELD!', '!ACCESS_RIGHT_FIELD!')";

	$qr_tree_insertGroupRights = "INSERT INTO UG_ACCESSRIGHTS(AR_ID, AR_PATH, AR_OBJECT_ID, AR_VALUE, AR_AUX) VALUES('!GROUP_ID_FIELD!', '!AR_PATH!', '!FOLDER_ID_FIELD!', '!AR_VALUE!', '')";

//	$qr_tree_insertGroupRights = "INSERT INTO TREE_GROUP_ACCESS_TABLE(GROUP_ID_FIELD, FOLDER_ID_FIELD, GROUP_ACCESS_RIGHT_FIELD) VALUES('!GROUP_ID_FIELD!', '!FOLDER_ID_FIELD!', '!GROUP_ACCESS_RIGHT_FIELD!')";

	$qr_tree_deleteUserFolderRights = "DELETE FROM U_ACCESSRIGHTS WHERE AR_ID='!USER_ID_FIELD!' AND AR_OBJECT_ID='!FOLDER_ID_FIELD!' AND AR_PATH='!AR_PATH!'";

//	$qr_tree_deleteUserFolderRights = "DELETE FROM TREE_ACCESS_TABLE WHERE USER_ID_FIELD='!USER_ID_FIELD!' AND FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_deleteGroupFolderRights = "DELETE FROM UG_ACCESSRIGHTS WHERE AR_ID='!GROUP_ID_FIELD!' AND AR_OBJECT_ID='!FOLDER_ID_FIELD!' AND AR_PATH='!AR_PATH!'";

//	$qr_tree_deleteGroupFolderRights = "DELETE FROM TREE_GROUP_ACCESS_TABLE WHERE GROUP_ID_FIELD='!GROUP_ID_FIELD!' AND FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectSystemUsers = "SELECT * FROM WBS_USER U, CONTACT C WHERE (U.U_STATUS = 0 OR U.U_STATUS = 2) AND C.C_ID=U.C_ID ORDER BY C.C_LASTNAME, C.C_FIRSTNAME, C.C_MIDDLENAME";

	$qr_tree_selectUserFolderRights = "SELECT ACCESS_RIGHT_FIELD FROM TREE_ACCESS_TABLE WHERE USER_ID_FIELD='!USER_ID_FIELD!' AND FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectUserSummaryFolderRights = "SELECT MAX(DGA.GROUP_ACCESS_RIGHT_FIELD) AS ACCESS_RIGHT_FIELD FROM TREE_GROUP_ACCESS_TABLE DGA, UGROUP_USER UGU WHERE UGU.U_ID = '!USER_ID_FIELD!' AND DGA.GROUP_ID_FIELD=UGU.UG_ID AND DGA.FOLDER_ID_FIELD='!FOLDER_ID_FIELD!' GROUP BY DGA.FOLDER_ID_FIELD";

	$qr_tree_selectUserGroupsSummaryFolderRights = "SELECT MAX(DGA.GROUP_ACCESS_RIGHT_FIELD) AS ACCESS_RIGHT_FIELD FROM TREE_GROUP_ACCESS_TABLE DGA WHERE DGA.FOLDER_ID_FIELD='!FOLDER_ID_FIELD!' AND DGA.UG_ID in (%s) GROUP BY DGA.FOLDER_ID_FIELD";

	$qr_tree_selectGroupFolderRights = "SELECT GROUP_ACCESS_RIGHT_FIELD FROM TREE_GROUP_ACCESS_TABLE WHERE GROUP_ID_FIELD='!GROUP_ID_FIELD!' AND FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectFolderUsers = "SELECT U.U_ID, UA.*, C.C_LASTNAME, C.C_FIRSTNAME, C.C_MIDDLENAME, C.C_EMAILADDRESS FROM U_ACCESSRIGHTS AS UA, CONTACT AS C, WBS_USER U WHERE UA.AR_PATH = '!AR_PATH!' AND UA.AR_OBJECT_ID = '!FOLDER_ID_FIELD!' AND C.C_ID=U.C_ID AND U.U_ID=UA.AR_ID AND U.U_STATUS <> 1 ORDER BY C_FIRSTNAME, C_LASTNAME, C_MIDDLENAME";

	$qr_tree_selectFolderGroupUsers = "SELECT U.U_ID, BIT_OR( UGA.AR_VALUE ) AS GROUPRIGHTS, UGA.AR_ID, C.C_LASTNAME, C.C_FIRSTNAME, C.C_MIDDLENAME, C.C_EMAILADDRESS FROM WBS_USER U, UGROUP_USER UGU, UG_ACCESSRIGHTS UGA, CONTACT C WHERE UGU.U_ID = U.U_ID AND UGA.AR_ID = UGU.UG_ID AND       UGA.AR_OBJECT_ID = '!FOLDER_ID_FIELD!' AND UGA.AR_PATH = '!AR_PATH!' AND U.U_STATUS <> 1 AND C.C_ID = U.C_ID GROUP BY UGU.U_ID ORDER BY C_FIRSTNAME, C_LASTNAME, C_MIDDLENAME";

	$qr_tree_selectFolderDocuments = "SELECT DL.*, A.ACCESS_RIGHT_FIELD FROM TREE_DOCUMENT_TABLE DL, TREE_ACCESS_TABLE A WHERE DL.FOLDER_ID_FIELD='!FOLDER_ID_FIELD!' AND A.FOLDER_ID_FIELD = DL.FOLDER_ID_FIELD AND DL.DOCUMENT_STATUS_FIELD='!DOCUMENT_STATUS_FIELD!' AND A.USER_ID_FIELD = '!USER_ID_FIELD!' ORDER BY %s";

	$qr_tree_selectFolderDocumentsIgnoreUser = "SELECT DL.* FROM TREE_DOCUMENT_TABLE DL WHERE DL.FOLDER_ID_FIELD='!FOLDER_ID_FIELD!' AND DL.DOCUMENT_STATUS_FIELD='!DOCUMENT_STATUS_FIELD!' ORDER BY %s %s";

	$qr_tree_selectRecycledDocuments = "SELECT DL.*, DF.FOLDER_NAME_FIELD FROM TREE_DOCUMENT_TABLE DL LEFT JOIN TREE_FOLDER_TABLE DF ON DF.FOLDER_ID_FIELD=DL.FOLDER_ID_FIELD WHERE DOCUMENT_STATUS_FIELD='!DOCUMENT_STATUS_FIELD!' AND DOCUMENT_MODIFIED_UID_FIELD='!DOCUMENT_MODIFIED_UID_FIELD!' ORDER BY %s %s";

	$qr_tree_selectAllRecycledDocuments = "SELECT DL.* FROM TREE_DOCUMENT_TABLE DL WHERE DOCUMENT_STATUS_FIELD='!DOCUMENT_STATUS_FIELD!' ORDER BY %s %s";

	$qr_tree_deleteFolder = "DELETE FROM TREE_FOLDER_TABLE WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_deleteFolderRights = "DELETE FROM U_ACCESSRIGHTS WHERE AR_OBJECT_ID='!FOLDER_ID_FIELD!' AND AR_PATH='!AR_PATH!'";
	
	$qr_tree_deleteFolderRightsLinks = "DELETE FROM ACCESSRIGHTS_LINK WHERE AR_OBJECT_ID='!FOLDER_ID_FIELD!' AND AR_PATH='!AR_PATH!'";

//	$qr_tree_deleteFolderRights = "DELETE FROM TREE_ACCESS_TABLE WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_deleteFolderDocuments = "DELETE FROM TREE_DOCUMENT_TABLE WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectDocument = "SELECT * FROM TREE_DOCUMENT_TABLE WHERE DOCUMENT_ID_FIELD='!DOCUMENT_ID_FIELD!'";

	$qr_tree_getMaxDocID = "SELECT MAX(DOCUMENT_ID_FIELD) FROM TREE_DOCUMENT_TABLE";

	$qr_tree_selectChildrenNum = "SELECT COUNT(*) FROM TREE_FOLDER_TABLE WHERE FOLDER_PARENT_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectSystemUsersFolderAccess = "SELECT U.*, C.*, UA.AR_VALUE FROM ( WBS_USER U, CONTACT C ) LEFT JOIN U_ACCESSRIGHTS UA ON UA.AR_OBJECT_ID='!FOLDER_ID_FIELD!' AND UA.AR_PATH = '!AR_PATH!' AND UA.AR_ID = U.U_ID WHERE C.C_ID=U.C_ID AND (U_STATUS = 0 OR U_STATUS = 2) ORDER BY C_FIRSTNAME, C_LASTNAME, C_MIDDLENAME";

//	$qr_tree_selectSystemUsersFolderAccess = "SELECT U.*, C.*, DA.ACCESS_RIGHT_FIELD FROM ( WBS_USER U, CONTACT C ) LEFT JOIN TREE_ACCESS_TABLE DA ON DA.FOLDER_ID_FIELD='!FOLDER_ID_FIELD!' AND DA.USER_ID_FIELD = U.U_ID WHERE C.C_ID=U.C_ID AND (U_STATUS = 0 OR U_STATUS = 2) ORDER BY C_FIRSTNAME, C_LASTNAME, C_MIDDLENAME";

	$qr_tree_selectSystemUserGroupsFolderAccess = "SELECT UG.*, UGA.AR_VALUE, COUNT(UGU.U_ID) AS USERCOUNT FROM UGROUP UG LEFT JOIN UG_ACCESSRIGHTS UGA ON UGA.AR_OBJECT_ID='!FOLDER_ID_FIELD!' AND UGA.AR_PATH = '!AR_PATH!' AND UGA.AR_ID = UG.UG_ID LEFT JOIN UGROUP_USER UGU ON UGU.UG_ID=UG.UG_ID GROUP BY UG.UG_ID ORDER BY UG.UG_NAME";

//	$qr_tree_selectSystemUserGroupsFolderAccess = "SELECT UG.*, DA.GROUP_ACCESS_RIGHT_FIELD AS ACCESS_RIGHT_FIELD, COUNT(UGU.U_ID) AS USERCOUNT FROM UGROUP UG LEFT JOIN TREE_GROUP_ACCESS_TABLE DA ON FOLDER_ID_FIELD='!FOLDER_ID_FIELD!' AND DA.GROUP_ID_FIELD = UG.UG_ID LEFT JOIN UGROUP_USER UGU ON UGU.UG_ID=UG.UG_ID GROUP BY UG.UG_ID ORDER BY UG.UG_NAME";

	$qr_tree_selectUserFolderCount = "SELECT COUNT(*) FROM TREE_FOLDER_TABLE DF, TREE_ACCESS_TABLE DA WHERE DA.FOLDER_ID_FIELD = DF.FOLDER_ID_FIELD AND DA.USER_ID_FIELD='!U_ID!' AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!'";

	$qr_tree_selectAllUserFolders = "SELECT DF.FOLDER_ID_FIELD FROM TREE_FOLDER_TABLE DF, TREE_ACCESS_TABLE DA WHERE DA.FOLDER_ID_FIELD = DF.FOLDER_ID_FIELD AND DA.USER_ID_FIELD='!U_ID!' AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!'";

	$qr_tree_selectFolderGroupCount = "SELECT COUNT(*) FROM UG_ACCESSRIGHTS UGA, UGROUP UG WHERE UG.UG_ID=UGA.AR_ID AND UGA.AR_PATH='!AR_PATH!' AND UGA.AR_OBJECT_ID='!AR_OBJECT_ID!' AND UGA.AR_VALUE<>0";

// 	$qr_tree_selectFolderGroupCount = "SELECT COUNT(*) FROM TREE_GROUP_ACCESS_TABLE DGA, UGROUP UG WHERE UG.UG_ID=DGA.GROUP_ID_FIELD AND DGA.FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectFolderUserCount = "SELECT COUNT(*) FROM U_ACCESSRIGHTS UA, WBS_USER U WHERE  UA.AR_OBJECT_ID='!AR_OBJECT_ID!' AND  UA.AR_PATH='!AR_PATH!' AND  U.U_ID=UA.AR_ID AND  U.U_STATUS <> 1 AND  U.U_ID <> '!U_ID!'";

// 	$qr_tree_selectFolderUserCount = "SELECT COUNT(*) FROM TREE_ACCESS_TABLE DA, WBS_USER U, CONTACT C WHERE (U.U_ACCESSTYPE='IND' OR U.U_ACCESSTYPE='SUM') AND C.C_ID=U.C_ID AND DA.FOLDER_ID_FIELD='!FOLDER_ID_FIELD!' AND U.U_ID=DA.USER_ID_FIELD AND U.U_STATUS <> 1 AND U.U_ID <> '!USER_ID_FIELD!'";

	$qr_tree_selectSummaryFolderCount = "SELECT COUNT(DISTINCT DF.FOLDER_ID_FIELD) FROM TREE_FOLDER_TABLE DF, TREE_GROUP_ACCESS_TABLE DA, UGROUP_USER UGU WHERE DA.FOLDER_ID_FIELD = DF.FOLDER_ID_FIELD AND DA.GROUP_ID_FIELD=UGU.UG_ID AND UGU.U_ID = '!U_ID!' AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!'";

	$qr_tree_selectAllSummaryFolders = "SELECT DISTINCT DF.FOLDER_ID_FIELD FROM TREE_FOLDER_TABLE DF, TREE_GROUP_ACCESS_TABLE DA, UGROUP_USER UGU WHERE DA.FOLDER_ID_FIELD = DF.FOLDER_ID_FIELD AND DA.GROUP_ID_FIELD=UGU.UG_ID AND UGU.U_ID = '!U_ID!' AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!'";

	$qr_tree_selectUserDocumentCount = "SELECT COUNT(*) FROM TREE_DOCUMENT_TABLE DL, TREE_FOLDER_TABLE DF, TREE_ACCESS_TABLE DA WHERE DA.FOLDER_ID_FIELD = DF.FOLDER_ID_FIELD AND DA.USER_ID_FIELD='!U_ID!' AND DL.FOLDER_ID_FIELD = DF.FOLDER_ID_FIELD";

	$qr_tree_selectAllUserDocuments = "SELECT DL.DOCUMENT_ID_FIELD FROM TREE_DOCUMENT_TABLE DL, TREE_FOLDER_TABLE DF, TREE_ACCESS_TABLE DA WHERE DA.FOLDER_ID_FIELD = DF.FOLDER_ID_FIELD AND DA.USER_ID_FIELD='!U_ID!' AND DL.FOLDER_ID_FIELD = DF.FOLDER_ID_FIELD";

	$qr_tree_selectUserSummaryDocumentCount = "SELECT COUNT(DISTINCT DL.DOCUMENT_ID_FIELD) FROM TREE_DOCUMENT_TABLE DL, TREE_GROUP_ACCESS_TABLE DGA, UGROUP_USER UGU WHERE UGU.U_ID='!U_ID!' AND DGA.GROUP_ID_FIELD=UGU.UG_ID AND DL.FOLDER_ID_FIELD = DGA.FOLDER_ID_FIELD";

	$qr_tree_selectAllUserSummaryDocuments = "SELECT DISTINCT DL.DOCUMENT_ID_FIELD FROM TREE_DOCUMENT_TABLE DL, TREE_GROUP_ACCESS_TABLE DGA, UGROUP_USER UGU WHERE UGU.U_ID='!U_ID!' AND DGA.GROUP_ID_FIELD=UGU.UG_ID AND DL.FOLDER_ID_FIELD = DGA.FOLDER_ID_FIELD";

	$qr_tree_selectUserRightCount = "SELECT COUNT(*) FROM TREE_ACCESS_TABLE WHERE USER_ID_FIELD='!U_ID!' AND ACCESS_RIGHT_FIELD='!ACCESS_RIGHT_FIELD!'";

	$qr_tree_selectFolderGroups = "SELECT UG.UG_ID, UGA.AR_VALUE, UG.UG_NAME FROM UG_ACCESSRIGHTS UGA, UGROUP UG WHERE UG.UG_ID=UGA.AR_ID AND UGA.AR_OBJECT_ID='!FOLDER_ID_FIELD!' AND UGA.AR_PATH='!AR_PATH!' ORDER BY UG.UG_NAME";

//	$qr_tree_selectFolderGroups = "SELECT UG.UG_ID, DGA.GROUP_ACCESS_RIGHT_FIELD AS ACCESS_RIGHT_FIELD, UG.UG_NAME FROM TREE_GROUP_ACCESS_TABLE DGA, UGROUP UG WHERE UG.UG_ID=DGA.GROUP_ID_FIELD AND DGA.FOLDER_ID_FIELD='!FOLDER_ID_FIELD!' ORDER BY UG.UG_NAME";

	$qr_tree_groupschunk = "AND UG.UG_ID IN (%s)";

	$qr_tree_select_folderStatus = "SELECT FOLDER_STATUS_FIELD FROM TREE_FOLDER_TABLE WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectfoldersplain = "SELECT * FROM TREE_FOLDER_TABLE WHERE FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' ORDER BY %s";

	$qr_tree_update_folderStatus = "UPDATE TREE_FOLDER_TABLE SET FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!' WHERE FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	$qr_tree_selectUserRightTotalMask  = "SELECT ( ( SELECT BIT_OR( U.AR_VALUE ) FROM U_ACCESSRIGHTS U WHERE U.AR_PATH = '!AR_PATH!' AND U.AR_ID = '!U_ID!' ) | ( SELECT BIT_OR( UG.AR_VALUE ) FROM UG_ACCESSRIGHTS UG, UGROUP_USER AS UGU WHERE UG.AR_ID=UGU.UG_ID AND UG.AR_PATH = '!AR_PATH!' AND UGU.U_ID='!U_ID!' ) ) AS RIGHT_VALUE";

	$qr_tree_selectUsersFoldersIds = "SELECT UA.AR_OBJECT_ID FROM U_ACCESSRIGHTS AS UA, TREE_FOLDER_TABLE DF WHERE UA.AR_PATH = '!AR_PATH!' AND UA.AR_VALUE <>0 AND UA.AR_ID = '!U_ID!' AND UA.AR_OBJECT_ID NOT IN ( 'AVAILABLEFOLDERS', 'VIEWSHARES' ) AND DF.FOLDER_ID_FIELD=UA.AR_OBJECT_ID AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!'";

	$qr_tree_selectGroupFoldersIds = "SELECT UGA.AR_OBJECT_ID FROM UG_ACCESSRIGHTS AS UGA, UGROUP_USER AS UGU, TREE_FOLDER_TABLE DF WHERE UGU.U_ID = '!U_ID!' AND UGU.UG_ID = UGA.AR_ID AND UGA.AR_PATH = '!AR_PATH!' AND UGA.AR_VALUE <>0 AND UGA.AR_OBJECT_ID NOT IN ( 'AVAILABLEFOLDERS', 'VIEWSHARES' ) AND DF.FOLDER_ID_FIELD=UGA.AR_OBJECT_ID AND DF.FOLDER_STATUS_FIELD='!FOLDER_STATUS_FIELD!'";

	$qr_tree_selectUserSummaryFoldersIds = "%s UNION %s";

	$qr_tree_selectUserDocumentsCount = "SELECT COUNT(*) AS CNT FROM TREE_DOCUMENT_TABLE DL WHERE DL.FOLDER_ID_FIELD  IN ( %s UNION %s )";



	//
	// Group support functions
	//

	$qr_group_selectGroupEntityCount = "SELECT COUNT(*) FROM TREE_DOCUMENT_TABLE D, TREE_FOLDER_TABLE G, GROUP_LINK_TABLE CGL WHERE CGL.GROUP_LINK_ENTITY_ID=D.DOCUMENT_ID_FIELD AND CGL.GROUP_LINK_GROUP_ID=G.FOLDER_ID_FIELD AND G.FOLDER_ID_FIELD='!FOLDER_ID_FIELD!'";

	//
	// Contact types
	//

	$qr_selectcontacttype = "SELECT * FROM CTYPE WHERE CT_ID='!CT_ID!'";

	//
	// Contacts
	//

	$qr_selectmaxc_id = "SELECT MAX(C_ID) FROM CONTACT";

	$qr_insertcontact = "INSERT INTO CONTACT(%s) VALUES (%s)";

	$qr_selectcontactfolder = "SELECT * FROM CFOLDER WHERE CF_ID='!CF_ID!'";

	$qr_updatecontact = "UPDATE CONTACT SET %s WHERE C_ID='!C_ID!'";

	$qr_selectusercontactdata = "SELECT CF.CT_ID, C.CF_ID, U.C_ID FROM CFOLDER CF, CONTACT C, WBS_USER U WHERE C.C_ID=U.C_ID AND CF.CF_ID=C.CF_ID AND U.U_ID='!U_ID!'";

	$qr_namesortclause = "UPPER(CONCAT(IF(C_FIRSTNAME is not null, C_FIRSTNAME, ''), IF(C_MIDDLENAME is not null, C_MIDDLENAME, ''), IF(C_LASTNAME is not null, C_LASTNAME, ''), IF(C_EMAILADDRESS is not null, C_EMAILADDRESS, '')))";

	$qr_deletecontact = "DELETE FROM CONTACT WHERE C_ID='!C_ID!'";

	$qr_selectcontact = "SELECT * FROM CONTACT WHERE C_ID='!C_ID!'";

	$qr_selectcontactanduser = "SELECT U.*, C.* FROM (CONTACT C) LEFT JOIN WBS_USER U ON U.C_ID=C.C_ID WHERE C.C_ID='!C_ID!'";

	$qr_selectcontactfieldvalue = "SELECT COUNT(*) FROM CONTACT WHERE TRIM(%s) = '!FIELD_VALUE!' AND C_ID <> '!C_ID!'";

	$qr_selectcontactfieldvalueNew = "SELECT COUNT(*) FROM CONTACT WHERE TRIM(%s) = '!FIELD_VALUE!'";

	$qr_selectFolderContacts = "SELECT C.*, U.U_STATUS, U.U_ID FROM CONTACT C LEFT JOIN WBS_USER U ON U.C_ID=C.C_ID WHERE CF_ID='%s' %s ORDER BY %s";

	$qr_selectFolderContactsWithEmails = "SELECT C.*, U.U_STATUS, U.U_ID FROM CONTACT C LEFT JOIN WBS_USER U ON U.C_ID=C.C_ID WHERE CF_ID='%s' AND C.C_EMAILADDRESS IS NOT NULL AND C.C_EMAILADDRESS <> \"\" %s ORDER BY %s";

	$qr_selectFolderContactNum = "SELECT COUNT(*) FROM CONTACT WHERE CF_ID='%s'";

	$qr_selectFolderContactWithEmailsNum = "SELECT COUNT(*) FROM CONTACT WHERE CF_ID='%s' AND C_EMAILADDRESS IS NOT NULL AND C_EMAILADDRESS <> \"\"";

	$qr_selectSystemGroupContacts = "SELECT U.*, C.* FROM WBS_USER U, CONTACT C WHERE U.U_STATUS='%s' AND C.C_ID=U.C_ID ORDER BY %s";

	$qr_selectGroupContacts = "SELECT U.*, C.* FROM WBS_USER U, CONTACT C, UGROUP_USER UGU WHERE U.U_ID=UGU.U_ID AND UGU.UG_ID = '%s' AND C.C_ID=U.C_ID AND U.U_STATUS <> '%s' ORDER BY %s";

	$qr_selectUserGroupContactCount = "SELECT COUNT(*) FROM WBS_USER U, CONTACT C, UGROUP_USER UGU WHERE U.U_ID=UGU.U_ID AND UGU.UG_ID = '!UG_ID!' AND C.C_ID=U.C_ID AND U.U_STATUS <> '!STATUS!'";

	$qr_selectSystemGroupContactCount = "SELECT COUNT(*) FROM WBS_USER U, CONTACT C WHERE U.U_STATUS='!STATUS!' AND C.C_ID=U.C_ID";

	$qr_selectContactUser = "SELECT U_ID FROM WBS_USER WHERE C_ID='!C_ID!'";

	$qr_totalRights = "(SELECT
							IF ( UA.AR_VALUE IS NULL, 0, UA.AR_VALUE ) |
							IF ( (SELECT BIT_OR( UG.AR_VALUE ) FROM UG_ACCESSRIGHTS AS UG, UGROUP_USER AS UGU WHERE UG.AR_ID=UGU.UG_ID
							AND UGU.U_ID=UA.AR_ID AND UA.AR_OBJECT_ID=UG.AR_OBJECT_ID AND UA.AR_PATH=UG.AR_PATH GROUP BY UGU.U_ID) IS NULL, 0,
							(SELECT BIT_OR( UG.AR_VALUE ) FROM UG_ACCESSRIGHTS AS UG, UGROUP_USER AS UGU WHERE UG.AR_ID=UGU.UG_ID
							AND UGU.U_ID=UA.AR_ID AND UA.AR_OBJECT_ID=UG.AR_OBJECT_ID AND UA.AR_PATH=UG.AR_PATH GROUP BY UGU.U_ID) )
							FROM U_ACCESSRIGHTS AS UA WHERE AR_ID='%2\$s' AND AR_PATH='/ROOT/CM/FOLDERS' AND AR_OBJECT_ID=CF.CF_ID)";

	$qr_select_foldersContactEmails = "(SELECT C.* FROM CONTACT C WHERE C.CF_ID IN (%1\$s) AND C_EMAILADDRESS IS NOT NULL AND LENGTH(C_EMAILADDRESS) > 0 %2\$s)";

	$qr_select_userGroupsContactEmails = "(SELECT C.* FROM WBS_USER U, CONTACT C, UGROUP_USER UGU WHERE U.U_ID=UGU.U_ID AND UGU.UG_ID IN (%1\$s) AND C.C_ID=U.C_ID AND U.U_STATUS <> 2 AND C_EMAILADDRESS IS NOT NULL AND LENGTH(C_EMAILADDRESS) > 0 %2\$s)";

	$qr_select_activeContactsEmails = "(SELECT C.* FROM WBS_USER U, CONTACT C WHERE C.C_ID=U.C_ID AND U.U_STATUS = 0 AND C_EMAILADDRESS IS NOT NULL AND LENGTH(C_EMAILADDRESS) > 0 %s)";

	$qr_select_inactiveContactsEmails = "(SELECT C.* FROM WBS_USER U, CONTACT C WHERE C.C_ID=U.C_ID AND U.U_STATUS = 2 AND C_EMAILADDRESS IS NOT NULL AND LENGTH(C_EMAILADDRESS) > 0 %s)";

	$qr_select_contactListsEmails = "(SELECT C.* FROM (CONTACT C, CLIST_CONTACT CC) LEFT JOIN WBS_USER U ON U.C_ID=C.C_ID WHERE C.C_ID=CC.C_ID AND C_EMAILADDRESS IS NOT NULL AND LENGTH(C_EMAILADDRESS) > 0 AND CC.CL_ID IN (%1\$s) %2\$s) UNION (SELECT C.* FROM CONTACT C, CLIST_GROUP CG, WBS_USER U, UGROUP_USER UG WHERE C.C_ID=U.C_ID AND CG.CL_ID IN (%1\$s) AND UG.U_ID=U.U_ID AND UG.UG_ID=CG.UG_ID AND C_EMAILADDRESS IS NOT NULL AND LENGTH(C_EMAILADDRESS) > 0 %2\$s) UNION (SELECT C.* FROM (CONTACT C, CLIST_FOLDER CLF) LEFT JOIN WBS_USER U ON U.C_ID=C.C_ID WHERE C.CF_ID=CLF.CF_ID AND CLF.CL_ID IN (%1\$s) AND C_EMAILADDRESS IS NOT NULL AND LENGTH(C_EMAILADDRESS) > 0  %2\$s)";

	$qr_select_indContactsListEmails = "(SELECT C.* FROM CONTACT C WHERE C.C_ID IN (%1\$s) AND C_EMAILADDRESS IS NOT NULL AND LENGTH(C_EMAILADDRESS) > 0 %2\$s)";

	$qr_select_avlbCnt_folders_chunk = "(SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS
											FROM (CONTACT CN, CFOLDER CF)
											LEFT JOIN WBS_USER U ON U.C_ID=CN.C_ID
											WHERE
											(%1\$s) AND
											CF.CF_ID=CN.CF_ID
											AND $qr_totalRights > 0)";


	$qr_select_avlbCnt_global_folders_chunk = "(SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS
											FROM (CONTACT CN, CFOLDER CF)
											LEFT JOIN WBS_USER U ON U.C_ID=CN.C_ID
											WHERE
											(%1\$s) AND
											CF.CF_ID=CN.CF_ID)";

	$qr_select_avlbCnt_users_chunk = "(SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS FROM WBS_USER U, CONTACT CN, CFOLDER CF WHERE (%1\$s) AND CN.C_ID=U.C_ID AND CF.CF_ID=CN.CF_ID)";

	$qr_select_avlbCnt_lists_chunk = "(SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS FROM (CONTACT CN, CLIST_CONTACT CC, CLIST CL, CFOLDER CF) LEFT JOIN WBS_USER U ON U.C_ID=CN.C_ID WHERE (%1\$s) AND CN.C_ID=CC.C_ID AND CC.CL_ID=CL.CL_ID AND CF.CF_ID=CN.CF_ID AND (CL.CL_SHARED OR CL.CL_OWNER_U_ID='%2\$s')) UNION (SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS FROM (CONTACT CN, CLIST_GROUP CG, WBS_USER U, UGROUP_USER UG, CFOLDER CF, CLIST CL) WHERE (%1\$s) AND CN.C_ID=U.C_ID AND CG.CL_ID=CL.CL_ID AND UG.U_ID=U.U_ID AND UG.UG_ID=CG.UG_ID AND CF.CF_ID=CN.CF_ID AND (CL.CL_SHARED OR CL.CL_OWNER_U_ID='%2\$s')) UNION (SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS FROM (CONTACT CN, CLIST_FOLDER CLF, CLIST CL, CFOLDER CF) LEFT JOIN WBS_USER U ON U.C_ID=CN.C_ID WHERE (%1\$s) AND CN.CF_ID=CLF.CF_ID AND CLF.CL_ID=CL.CL_ID AND CF.CF_ID=CN.CF_ID AND (CL.CL_SHARED OR CL.CL_OWNER_U_ID='%2\$s'))";

	$qr_select_avlbCnt_global_lists_chunk = "(SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS FROM (CONTACT CN, CLIST_CONTACT CC, CLIST CL, CFOLDER CF) LEFT JOIN WBS_USER U ON U.C_ID=CN.C_ID WHERE (%1\$s) AND CN.C_ID=CC.C_ID AND CC.CL_ID=CL.CL_ID AND CF.CF_ID=CN.CF_ID) UNION (SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS FROM (CONTACT CN, CLIST_GROUP CG, WBS_USER U, UGROUP_USER UG, CFOLDER CF, CLIST CL) WHERE (%1\$s) AND CN.C_ID=U.C_ID AND CG.CL_ID=CL.CL_ID AND UG.U_ID=U.U_ID AND UG.UG_ID=CG.UG_ID AND CF.CF_ID=CN.CF_ID) UNION (SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS FROM (CONTACT CN, CLIST_FOLDER CLF, CLIST CL, CFOLDER CF) LEFT JOIN WBS_USER U ON U.C_ID=CN.C_ID WHERE (%1\$s) AND CN.CF_ID=CLF.CF_ID AND CLF.CL_ID=CL.CL_ID AND CF.CF_ID=CN.CF_ID)";

	$qr_set_contact_subscriber = "UPDATE CONTACT SET C_SUBSCRIBER='!C_SUBSCRIBER!' WHERE C_ID='!C_ID!'";

	$qr_contact_confirmed_filter = "AND NOT EXISTS(SELECT * FROM UNSUBSCRIBER WHERE LOWER(C.C_EMAILADDRESS) = LOWER(ENS_EMAIL))";

	$qr_insert_unsubscriber = "INSERT INTO UNSUBSCRIBER(ENS_EMAIL, ENS_DATETIME) VALUES ('!ENS_EMAIL!', NOW())";

	$qr_delete_unsubscriber = "DELETE FROM UNSUBSCRIBER WHERE LOWER(ENS_EMAIL)='!ENS_EMAIL!'";

	$qr_select_contact_address = "SELECT C_EMAILADDRESS FROM CONTACT WHERE C_ID='!C_ID!'";

	$qr_select_unsubscriber_cnt = "SELECT COUNT(*) FROM UNSUBSCRIBER U, CONTACT C WHERE LOWER(ENS_EMAIL)=LOWER(C.C_EMAILADDRESS) AND C.C_ID='!C_ID!'";

	$qr_find_users = "SELECT CN.*, CF.CF_NAME, U.U_ID, U.U_STATUS FROM ( CONTACT CN, CFOLDER CF, WBS_USER U ) WHERE U.C_ID=CN.C_ID AND (%1\$s) AND CF.CF_ID=CN.CF_ID";

	$qr_selectSpecificContacts = "SELECT C.*, U.U_ID, CF.CF_NAME, U.U_STATUS FROM (CONTACT C, CFOLDER CF) LEFT JOIN WBS_USER U ON U.C_ID=C.C_ID WHERE CF.CF_ID=C.CF_ID AND C.C_ID in (%s) ORDER BY %s";

	$qr_setFolderType = "UPDATE CFOLDER SET CT_ID='!CT_ID!' WHERE CF_ID='!CF_ID!'";

	//
	// File formats
	//

	$qr_selectmaxfif_id = "SELECT MAX(FIF_ID) FROM FILE_IMPORT_FORMAT WHERE FIF_LIST='!FIF_LIST!'";

	$qr_insertFileFormat = "INSERT INTO FILE_IMPORT_FORMAT(FIF_ID, FIF_LIST, FIF_NAME, FIF_SETTINGS, FIF_OWNER_U_ID) VALUES ('!FIF_ID!', '!FIF_LIST!', '!FIF_NAME!', '!FIF_SETTINGS!', '!FIF_OWNER_U_ID!')";

	$qr_selectFileFormats = "SELECT * FROM FILE_IMPORT_FORMAT WHERE FIF_LIST='!FIF_LIST!' AND (FIF_OWNER_U_ID='!U_ID!' OR FIF_OWNER_U_ID IS NULL) ORDER BY FIF_NAME";

	$qr_selectSystemFileFormats = "SELECT * FROM FILE_IMPORT_FORMAT WHERE FIF_LIST='!FIF_LIST!' AND FIF_OWNER_U_ID IS NULL ORDER BY FIF_NAME";

	$qr_selectUserFileFormats = "SELECT * FROM FILE_IMPORT_FORMAT WHERE FIF_LIST='!FIF_LIST!' AND FIF_OWNER_U_ID='!U_ID!' ORDER BY FIF_NAME";

	$qr_selectFileFormat = "SELECT * FROM FILE_IMPORT_FORMAT WHERE FIF_LIST='!FIF_LIST!' AND FIF_ID='!FIF_ID!'";

	$qr_deleteFileFormat = "DELETE FROM FILE_IMPORT_FORMAT WHERE FIF_LIST='!FIF_LIST!' AND FIF_ID='!FIF_ID!'";

	$qr_updateFileFormatName = "UPDATE FILE_IMPORT_FORMAT SET FIF_NAME='!FIF_NAME!' WHERE FIF_LIST='!FIF_LIST!' AND FIF_ID='!FIF_ID!'";

	//
	// Application settings
	//

	$qr_selectAppSettings = "SELECT SETTINGS FROM APPSETTINGS WHERE APP_ID='!APP_ID!'";

	$qr_insertAppSettings = "INSERT INTO APPSETTINGS(APP_ID, SETTINGS) VALUES ('!APP_ID!', '!SETTINGS!')";

	$qr_deleteAppSettings = "DELETE FROM APPSETTINGS WHERE APP_ID='!APP_ID!'";

	//
	// SMS
	//

	$qr_selectSMSSum = "SELECT SUM(SMSH_CHARGE) AS SM, SMSH_CHARGED FROM SMS_HISTORY GROUP BY SMSH_CHARGED";

	$qr_selectSMSSumTo = "SELECT SUM(SMSH_CHARGE) AS SM, SMSH_CHARGED FROM SMS_HISTORY WHERE SMSH_DATETIME <= '!TO!' GROUP BY SMSH_CHARGED";

	$qr_selectSMSSumFrom = "SELECT SUM(SMSH_CHARGE) AS SM, SMSH_CHARGED FROM SMS_HISTORY WHERE SMSH_DATETIME >= '!FROM!' GROUP BY SMSH_CHARGED";

	$qr_selectSMSSumBetween = "SELECT SUM(SMSH_CHARGE) AS SM, SMSH_CHARGED FROM SMS_HISTORY WHERE SMSH_DATETIME >= '!FROM!' AND SMSH_DATETIME <= '!TO!' GROUP BY SMSH_CHARGED";

	$qr_selectSMSHistory = "SELECT * FROM SMS_HISTORY ORDER BY SMSH_DATETIME DESC";

	$qr_selectSMSHistoryTo = "SELECT * FROM SMS_HISTORY WHERE SMSH_DATETIME <= '!TO!' ORDER BY SMSH_DATETIME DESC";

	$qr_selectSMSHistoryFrom = "SELECT * FROM SMS_HISTORY WHERE SMSH_DATETIME >= '!FROM!' ORDER BY SMSH_DATETIME DESC";

	$qr_selectSMSHistoryBetween = "SELECT * FROM SMS_HISTORY WHERE SMSH_DATETIME >= '!FROM!' AND SMSH_DATETIME <= '!TO!' ORDER BY SMSH_DATETIME DESC";

	$qr_selectSMSUserHistory = "SELECT * FROM SMS_HISTORY WHERE SMSH_USER_ID='!U_ID!' ORDER BY SMSH_DATETIME DESC";

	$qr_selectSMSUserHistoryTo = "SELECT * FROM SMS_HISTORY WHERE SMSH_USER_ID='!U_ID!' AND SMSH_DATETIME <= '!TO!' ORDER BY SMSH_DATETIME DESC";

	$qr_selectSMSUserHistoryFrom = "SELECT * FROM SMS_HISTORY WHERE SMSH_USER_ID='!U_ID!' AND SMSH_DATETIME >= '!FROM!' ORDER BY SMSH_DATETIME DESC";

	$qr_selectSMSUserHistoryBetween = "SELECT * FROM SMS_HISTORY WHERE SMSH_USER_ID='!U_ID!' AND SMSH_DATETIME >= '!FROM!' AND SMSH_DATETIME <= '!TO!' ORDER BY SMSH_DATETIME DESC";

	$qr_addSMSHistory = "INSERT INTO SMS_HISTORY ( SMSH_DATETIME, SMSH_USER_ID, SMSH_PHONE, SMSH_WIDTH, SMSH_QTY, SMSH_APP, SMSH_MODULEID, SMSH_TEXT, SMSH_MSGID, SMSH_CHARGE, SMSH_CHARGED, SMSH_UNLIM, SMSH_STATUS, SMSH_STATUS_TEXT ) VALUES ( NOW(), '!SMSH_USER_ID!', '!SMSH_PHONE!', '!SMSH_WIDTH!', '!SMSH_QTY!', '!SMSH_APP!', '!SMSH_MODULEID!', '!SMSH_TEXT!', '!SMSH_MSGID!', '!SMSH_CHARGE!', '!SMSH_CHARGED!', '!SMSH_UNLIM!', '!SMSH_STATUS!', '!SMSH_STATUS_TEXT!' )";

	$qr_modSMSHistory = "UPDATE SMS_HISTORY SET SMSH_DATETIME='!SMSH_DATETIME!', SMSH_USER_ID='!SMSH_USER_ID!', SMSH_PHONE='!SMSH_PHONE!' , SMSH_WIDTH='!SMSH_WIDTH!', SMSH_QTY='!SMSH_QTY!', SMSH_APP='!SMSH_APP!', SMSH_MODULEID='!SMSH_MODULEID!', SMSH_TEXT='!SMSH_TEXT!', SMSH_MSGID='!SMSH_MSGID!', SMSH_CHARGE='!SMSH_CHARGE!', SMSH_CHARGED='!SMSH_CHARGED!', SMSH_UNLIM='!SMSH_UNLIM!', SMSH_STATUS='!SMSH_STATUS!', SMSH_STATUS_TEXT='!SMSH_STATUS_TEXT!' WHERE SMSH_ID='!SMSH_ID!'";

	$qr_getSMS = "SELECT * FROM SMS_HISTORY WHERE SMSH_ID='!SMSH_ID!'";

	$qr_selectSMSUserHistoryStatus = "SELECT *, UNIX_TIMESTAMP(SMSH_DATETIME) AS TIMESTAMP FROM SMS_HISTORY WHERE SMSH_USER_ID='!U_ID!' AND SMSH_STATUS='!SMSH_STATUS!' ORDER BY SMSH_DATETIME DESC";

	$qr_selectSMSHistoryStatus = "SELECT *, UNIX_TIMESTAMP(SMSH_DATETIME) AS TIMESTAMP FROM SMS_HISTORY WHERE SMSH_STATUS='!SMSH_STATUS!' ORDER BY SMSH_DATETIME DESC";

	$qr_getSMSHistoryCount = "SELECT COUNT(*) FROM SMS_HISTORY";

	$qr_getSMSUserHistoryCount = "SELECT COUNT(*) FROM SMS_HISTORY WHERE SMSH_USER_ID='!U_ID!'";

	$qr_selectSMSBalanceHistory = "SELECT * FROM SMS_CREDIT_HISTORY ORDER BY SMSG_DATETIME DESC";

	$qr_selectSMSUserBalanceHistory = "SELECT * FROM SMS_CREDIT_HISTORY WHERE SMSG_USER_ID='!U_ID!' ORDER BY SMSG_DATETIME DESC";

	$qr_addSMSBalanceHistoryRecord = "INSERT INTO SMS_CREDIT_HISTORY ( SMSG_DATETIME, SMSG_USER_ID, SMSG_QTY, SMSG_QS, SMSG_SOURCE ) VALUES ( NOW(), '!SMSG_USER_ID!', '!SMSG_QTY!', '!SMSG_QS!', '!SMSG_SOURCE!' )";

	$qr_getSMSBalance = "SELECT * FROM SMS_BALANCE WHERE SMS_USER_ID='!SMS_USER_ID!'";

	$qr_addSMSBalance = "INSERT INTO SMS_BALANCE ( SMS_USER_ID, SMS_SENT, SMS_BALANCE ) VALUES ( '!SMS_USER_ID!', '!SMS_SENT!', '!SMS_BALANCE!' )";

	$qr_modSMSBalance = "UPDATE SMS_BALANCE SET SMS_SENT='!SMS_SENT!', SMS_BALANCE='!SMS_BALANCE!' WHERE SMS_USER_ID='!SMS_USER_ID!'";

	$qr_getSMSUsersBalance = "SELECT U.U_ID, C.C_FIRSTNAME, C.C_LASTNAME, B.SMS_BALANCE, B.SMS_SENT FROM WBS_USER U LEFT JOIN CONTACT C ON U.C_ID=C.C_ID LEFT JOIN SMS_BALANCE B ON U.U_ID=B.SMS_USER_ID WHERE U.U_STATUS<>1 ORDER BY U.U_ID";

	$qr_subSMSUserBalance = "UPDATE SMS_BALANCE SET SMS_BALANCE=IF(SMS_BALANCE IS NULL, NULL, SMS_BALANCE-'!QTY!'), SMS_SENT=SMS_SENT+'!QTY!' WHERE SMS_USER_ID='!SMS_USER_ID!'";

	$qr_subSMSSystemBalance = "UPDATE SMS_BALANCE SET SMS_BALANCE=IF(SMS_BALANCE IS NULL, NULL, SMS_BALANCE-'!CHARGE!') WHERE SMS_USER_ID='!SYS_USER!'";

	//
	// Quotas
	//

	$qr_select_disk_usage_record_count = "SELECT COUNT(*) AS CNT FROM DISK_USAGE WHERE DU_USER_ID='!U_ID!' AND DU_APP_ID='!APP_ID!'";

	$qr_update_disk_usage_record = "UPDATE DISK_USAGE SET DU_SIZE=IF(DU_SIZE+('!SIZE!') >= 0, DU_SIZE+('!SIZE!'), 0) WHERE DU_USER_ID='!U_ID!' AND DU_APP_ID='!APP_ID!'";

	$qr_insert_disk_usage_record = "INSERT INTO DISK_USAGE(DU_USER_ID, DU_APP_ID, DU_SIZE) VALUES ('!U_ID!', '!APP_ID!', 0)";

	$qr_list_user_disk_quotas = "SELECT * FROM USER_DISK_QUOTA WHERE UDQ_USER_ID='!U_ID!'";

	$qr_insert_user_app_disk_quota = "REPLACE INTO USER_DISK_QUOTA SET UDQ_USER_ID='!UDQ_USER_ID!', UDQ_APP_ID='!UDQ_APP_ID!', UDQ_SIZE='!UDQ_SIZE!'";

	$qr_delete_user_app_disk_quota = "DELETE FROM USER_DISK_QUOTA WHERE UDQ_USER_ID='!UDQ_USER_ID!' AND UDQ_APP_ID='!UDQ_APP_ID!'";

	$qr_select_user_app_quota = "SELECT UDQ_SIZE FROM USER_DISK_QUOTA WHERE UDQ_USER_ID='!UDQ_USER_ID!' AND UDQ_APP_ID='!UDQ_APP_ID!'";

	$qr_select_disk_usage_record = "SELECT DU_SIZE AS CNT FROM DISK_USAGE WHERE DU_USER_ID='!U_ID!' AND DU_APP_ID='!APP_ID!'";

	$qr_select_disk_usage_total = "SELECT SUM(DU_SIZE) FROM DISK_USAGE";

	//
	// Contact lists
	//

	$qr_select_max_clist_id = "SELECT MAX(CL_ID) FROM CLIST";

	$qr_insert_clist = "INSERT INTO CLIST(CL_ID, CL_NAME, CL_MODIFYDATETIME, CL_MODIFYUSERNAME, CL_OWNER_U_ID, CL_SHARED) VALUES ('!CL_ID!', '!CL_NAME!', NOW(), '!CL_MODIFYUSERNAME!', '!CL_OWNER_U_ID!', '!CL_SHARED!')";

	$qr_update_clist = "UPDATE CLIST SET CL_NAME='!CL_NAME!', CL_MODIFYDATETIME=NOW(), CL_MODIFYUSERNAME='!CL_MODIFYUSERNAME!', CL_OWNER_U_ID='!CL_OWNER_U_ID!', CL_SHARED='!CL_SHARED!' WHERE CL_ID='!CL_ID!'";
	
	$qr_select_contactlists = "SELECT * FROM CLIST ORDER BY %s";

	$qr_select_contactlistsforcontact = "SELECT * FROM CLIST CL, CLIST_CONTACT CLC WHERE CLC.C_ID='%s' AND CL.CL_ID=CLC.CL_ID ORDER BY %s";

	$qr_select_accessablecontactlists = "SELECT * FROM CLIST WHERE CL_SHARED=1 OR CL_OWNER_U_ID='%s' ORDER BY %s";

	$qr_select_contactlistnum = "SELECT COUNT(*) FROM CLIST";

	$qr_select_clist = "SELECT * FROM CLIST WHERE CL_ID='!CL_ID!'";

	$qr_delete_clist_folders = "DELETE FROM CLIST_FOLDER WHERE CL_ID='!CL_ID!'";

	$qr_insert_clist_folder = "INSERT INTO CLIST_FOLDER(CL_ID, CF_ID) VALUES ('!CL_ID!', '!CF_ID!')";

	$qr_delete_clist_groups = "DELETE FROM CLIST_GROUP WHERE CL_ID='!CL_ID!'";

	$qr_select_clist_groups = "SELECT CLG.UG_ID FROM CLIST_GROUP CLG, UGROUP UG WHERE CLG.CL_ID='!CL_ID!' AND UG.UG_ID=CLG.UG_ID";

	$qr_insert_clist_group = "INSERT INTO CLIST_GROUP(CL_ID, UG_ID) VALUES ('!CL_ID!', '!UG_ID!')";

	$qr_select_clist_folders = "SELECT CLF.CF_ID FROM CLIST_FOLDER CLF, CFOLDER CF WHERE CLF.CL_ID='!CL_ID!' AND CF.CF_ID=CLF.CF_ID";

	$qr_select_clist_contacts = "SELECT CL.C_ID FROM CLIST_CONTACT CL, CONTACT C WHERE CL_ID='!CL_ID!' AND C.C_ID=CL.C_ID ORDER BY UPPER(CONCAT(IF(C_FIRSTNAME is not null, C_FIRSTNAME, ''), IF(C_MIDDLENAME is not null, C_MIDDLENAME, ''), IF(C_LASTNAME is not null, C_LASTNAME, ''), IF(C_EMAILADDRESS is not null, C_EMAILADDRESS, '')))";

	$qr_delete_clist_contact = "DELETE FROM CLIST_CONTACT WHERE CL_ID='!CL_ID!' AND C_ID='!C_ID!'";

	$qr_insert_clist_contact = "INSERT INTO CLIST_CONTACT(C_ID, CL_ID, CLC_MODIFYDATETIME, CLC_MODIFYUSERNAME) VALUES ('!C_ID!', '!CL_ID!', NOW(), '!CLC_MODIFYUSERNAME!')";

	$qr_select_basic_clist_contacts = "(SELECT C.*, U.U_ID, U.U_STATUS FROM (CONTACT C, CLIST_CONTACT CC) LEFT JOIN WBS_USER U ON U.C_ID=C.C_ID WHERE C.C_ID=CC.C_ID AND CC.CL_ID='%1\$s') UNION (SELECT C.*, U.U_ID, U.U_STATUS FROM CONTACT C, CLIST_GROUP CG, WBS_USER U, UGROUP_USER UG WHERE C.C_ID=U.C_ID AND CG.CL_ID='%1\$s' AND UG.U_ID=U.U_ID AND UG.UG_ID=CG.UG_ID) UNION (SELECT C.*, U.U_ID, U.U_STATUS FROM (CONTACT C, CLIST_FOLDER CLF) LEFT JOIN WBS_USER U ON U.C_ID=C.C_ID WHERE C.CF_ID=CLF.CF_ID AND CLF.CL_ID='%1\$s')";

	$qr_select_clist_contacts_num = "SELECT COUNT(*) FROM CONTACT C, CLIST_CONTACT CC WHERE C.C_ID=CC.C_ID AND CC.CL_ID='!CL_ID!'";

	$qr_select_clist_foldercontacts_num = "SELECT COUNT(*) FROM CONTACT C, CLIST_GROUP CG, WBS_USER U, UGROUP_USER UG WHERE C.C_ID=U.C_ID AND CG.CL_ID='!CL_ID!' AND UG.U_ID=U.U_ID AND UG.UG_ID=CG.UG_ID";

	$qr_select_clist_groupcontacts_num = "SELECT COUNT(*) FROM CONTACT C, CLIST_FOLDER CLF WHERE C.CF_ID=CLF.CF_ID AND CLF.CL_ID='!CL_ID!'";

	$qr_select_clist_contact_num = "SELECT COUNT(*) FROM CONTACT C, CLIST_CONTACT CC WHERE CC.C_ID=C.C_ID AND C.C_ID='!C_ID!' AND CC.CL_ID='!CL_ID!'";

	$qr_delete_user_lists = "DELETE FROM CLIST WHERE (CL_SHARED IS NULL OR CL_SHARED <> 1) AND CL_OWNER_U_ID='!U_ID!'";

	$qr_select_user_list_access = "SELECT COUNT(*) FROM CLIST WHERE CL_ID='!CL_ID!' AND (CL_SHARED=1 OR CL_OWNER_U_ID = '!U_ID!')";

	$qr_delete_clist_contacts = "DELETE FROM CLIST_CONTACT WHERE CL_ID='!CL_ID!'";

	$qr_delete_contact_from_lists = "DELETE FROM CLIST_CONTACT WHERE C_ID='!C_ID!'";

	$qr_delete_clist = "DELETE FROM CLIST WHERE CL_ID='!CL_ID!'";

	$qr_insertMailAccount = "INSERT INTO MMACCOUNT (MMA_NAME, MMA_EMAIL, MMA_DOMAIN, MMA_PASSWORD, MMA_INTERNAL, MMA_USERID) VALUES ('!MMA_NAME!', '!MMA_EMAIL!', '!MMA_DOMAIN!', '!MMA_PASSWORD!', '!MMA_INTERNAL!', '!MMA_USERID!')";

?>
